package com.ruoyi.recipe.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.recipe.domain.RecipeTag;
import com.ruoyi.recipe.domain.RecipeTagCategory;
import com.ruoyi.recipe.mapper.RecipeTagCategoryMapper;
import com.ruoyi.system.domain.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.recipe.mapper.RecipeTagMapper;
import com.ruoyi.recipe.service.IRecipeTagService;
import com.ruoyi.common.core.text.Convert;

/**
 * 食谱类别标签Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-10
 */
@Service
public class RecipeTagServiceImpl implements IRecipeTagService 
{
    @Autowired
    private RecipeTagMapper recipeTagMapper;
    @Autowired
    private RecipeTagCategoryMapper recipeTagCategoryMapper;

    /**
     * 查询食谱类别标签
     * 
     * @param tagId 食谱类别标签主键
     * @return 食谱类别标签
     */
    @Override
    public RecipeTag selectRecipeTagById(Long tagId)
    {
        return recipeTagMapper.selectRecipeTagById(tagId);
    }

    /**
     * 查询食谱类别标签列表
     * 
     * @param recipeTag 食谱类别标签
     * @return 食谱类别标签
     */
    @Override
    public List<RecipeTag> selectRecipeTagList(RecipeTag recipeTag)
    {
        return recipeTagMapper.selectRecipeTagList(recipeTag);
    }

    /**
     * 新增食谱类别标签
     * 
     * @param recipeTag 食谱类别标签
     * @return 结果
     */
    @Override
    public int insertRecipeTag(RecipeTag recipeTag)
    {
        recipeTag.setCreateTime(DateUtils.getNowDate());
        return recipeTagMapper.insertRecipeTag(recipeTag);
    }

    /**
     * 修改食谱类别标签
     * 
     * @param recipeTag 食谱类别标签
     * @return 结果
     */
    @Override
    public int updateRecipeTag(RecipeTag recipeTag)
    {
        recipeTag.setUpdateTime(DateUtils.getNowDate());
        return recipeTagMapper.updateRecipeTag(recipeTag);
    }

    /**
     * 批量删除食谱类别标签
     * 
     * @param tagIds 需要删除的食谱类别标签主键
     * @return 结果
     */
    @Override
    public int deleteRecipeTagByTagIds(String tagIds)
    {
        return recipeTagMapper.deleteRecipeTagByTagIds(Convert.toStrArray(tagIds));
    }

    /**
     * 删除食谱类别标签信息
     * 
     * @param tagId 食谱类别标签主键
     * @return 结果
     */
    @Override
    public int deleteRecipeTagByTagId(Long tagId)
    {
        return recipeTagMapper.deleteRecipeTagByTagId(tagId);
    }

    /**
     * 校验食谱类别标签名称
     *
     * @param recipeTag 食谱类别标签名称
     * @return 结果
     */
    public boolean checkTagNameUnique(RecipeTag recipeTag)
    {
        Long id = StringUtils.isNull(recipeTag.getTagId()) ? -1L : recipeTag.getTagId();
        RecipeTag info = recipeTagMapper.checkTagNameUnique(recipeTag.getTagName());
        if (StringUtils.isNotNull(info) && info.getTagId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 根据条件分页查询已分配标签列表
     *
     * @param recipeTag 标签
     * @return 标签集合信息
     */
    @Override
    public List<RecipeTag> selectAllocatedList(RecipeTag recipeTag)
    {
        return recipeTagMapper.selectAllocatedList(recipeTag);
    }

    @Override
    public List<RecipeTag> selectTagAll() {
        return recipeTagMapper.selectTagAll();
    }

    /**
     * 批量分配类别
     *
     * @param tagId 标签ID
     * @param categoryIds 类别ID
     * @return 结果
     */
    @Override
    public int insertAllocateCategory(Long tagId, String categoryIds)
    {
        Long[] categories = Convert.toLongArray(categoryIds);
        // 新增用户与角色管理
        List<RecipeTagCategory> list = new ArrayList<RecipeTagCategory>();
        for (Long categoryId : categories)
        {
            RecipeTagCategory ur = new RecipeTagCategory();
            ur.setCategoryId(categoryId);
            ur.setTagId(tagId);
            list.add(ur);
        }
        return recipeTagCategoryMapper.batchTagCategory(list);
    }

    @Override
    public int cancelAllocateCategory(RecipeTagCategory tagCategory) {
        return recipeTagCategoryMapper.deleteAllocateCategory(tagCategory);
    }

    @Override
    public int cancelAllocateCategories(Long tagId, String categoryIds) {
        return recipeTagCategoryMapper.deleteAllocateCategories(tagId, Convert.toLongArray(categoryIds));
    }
}
